package com.atguigu.day05;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.MySinkFunction;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/4/3
 * 该案例演示了自定义SinkFunciton  将流中数据写到MySQL
 */
public class Flink03_Sink_MySQL_Self {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对流中数据进行类型转换    String-->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = socketDS.map(new WaterSensorMapFunction());
        //TODO 4.将流中数据写到Mysql表中
        wsDS.addSink(new MySinkFunction());
        env.execute();
    }
}
